package com.arcblock.demo.adapter


import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.arcblock.demo.R
import com.arcblock.demo.constant.Constant.Companion.BASE_URL
import com.arcblock.demo.entity.Frontmatter
import com.arcblock.demo.inter.IOnItemClickListener
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy

/**
 * 首页列表适配器
 */
class MainAdapter(private val exampleList: List<Frontmatter>, var context: Context,var itemClick: IOnItemClickListener) :
    RecyclerView.Adapter<MainAdapter.ExampleViewHolder>() {

    class ExampleViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
         var image_view: ImageView = itemView.findViewById(R.id.image_view)
         var title: TextView = itemView.findViewById(R.id.title)
         var tag_views: LinearLayout = itemView.findViewById(R.id.tag_views)
         var item_layout: LinearLayout = itemView.findViewById(R.id.item_layout)
         var categories: TextView = itemView.findViewById(R.id.categories)
         var date: TextView = itemView.findViewById(R.id.date)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ExampleViewHolder {
        val itemView =
            LayoutInflater.from(parent.context).inflate(R.layout.list_item, parent, false)
        return ExampleViewHolder(itemView)
    }

    override fun getItemCount(): Int {
        return exampleList.size
    }


    override fun onBindViewHolder(holder: ExampleViewHolder, position: Int) {
        var bean:Frontmatter = exampleList[position]
        //为TextView设置内容
        holder.title.text = bean?.frontmatter?.title?:""
        //多个分类中间加|
        if(bean?.frontmatter?.categories?.size>0){
            if(bean.frontmatter.categories.size==1){
                var cateText = bean.frontmatter.categories[0]
                holder.categories.text = "分类：" + cateText
            }else{
                var cateText = "分类："
                for(element in bean.frontmatter.categories){
                    cateText+=element+"|"
                }
                holder.categories.text = cateText.substring(0,cateText.length-1)
            }
        }

        var imagePath = bean?.frontmatter?.banner?.childImageSharp?.fixed?.src?:""
        //缓存所有尺寸图片，并压缩图片，节省内存空间
        Glide.with(context).asBitmap().diskCacheStrategy(DiskCacheStrategy.ALL)// 缓存所有尺寸的图片
            .thumbnail( 0.1f ).load(BASE_URL+imagePath).placeholder(R.mipmap.ic_launcher).into(holder.image_view)
        holder.date.text = bean?.frontmatter?.date?:""
        holder.tag_views.removeAllViews();
        if(bean?.frontmatter?.tags?.size>0) {
            for (element in bean.frontmatter.tags) {
                val inflater = LayoutInflater.from(context)
                var view:View = inflater.inflate(R.layout.tag_item,null);
                var textView:TextView = view.findViewById<TextView>(R.id.tag)
                textView.text = element
                holder.tag_views.addView(view)
            }
        }
        holder.item_layout.setOnClickListener(View.OnClickListener {
            if(itemClick!=null) {
                itemClick.onItemClick(bean?.frontmatter?.path ?: "")
            }
        })
    }

}